/**
 * jims
 */
package com.jims.master.modules.nurse.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.service.PatMasterIndexService;
import com.jims.master.modules.doctor.service.PatVisitService;
import com.jims.master.modules.nurse.entity.BedRec;
import com.jims.master.modules.nurse.service.BedRecService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.nurse.entity.NewbornRec;
import com.jims.master.modules.nurse.service.NewbornRecService;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 新生儿记录Controller
 * @author 赵宁
 * @version 2017-04-10
 */
@Controller
@RequestMapping(value = "${adminPath}/nurse/newbornRec")
public class NewbornRecController extends BaseController {

	@Autowired
	private NewbornRecService newbornRecService;
	@Autowired
    private BedRecService bedRecService;
	@Autowired
    private PatVisitService patVisitService;
	
	@ModelAttribute
	public NewbornRec get(@RequestParam(required=false) String id) {
		NewbornRec entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = newbornRecService.get(id);
		}
		if (entity == null){
			entity = new NewbornRec();
		}
		return entity;
	}


	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(NewbornRec newbornRec,  Model model,HttpServletRequest request,HttpServletResponse response) {
		String wardCode= UserUtils.getUser().getOrgDept().getId();
		newbornRec.setWardCode(wardCode);
		Page<BaseDto> page=newbornRecService.getPatientList(new Page<BaseDto>(request,response),newbornRec);
		model.addAttribute("newbornRec",newbornRec);
		model.addAttribute("page", page);
		return  "modules/nurse/newBornRegist";
	}

    /**
     * 新生儿登记页面
     * @param newbornRec
     * @param model
     * @return String
     * @author  xueliang
     * @update by CTQ at 2017-07-21 17:19:31
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(NewbornRec newbornRec, Model model) {
        newbornRec.updateOrgId();
        newbornRec = newbornRecService.findNewBornInfo(newbornRec);
        newbornRec.setBirthCertifiaationNo(null);
        newbornRec.setId(null);
        model.addAttribute("newBorn",newbornRec);
        return "modules/nurse/newBornInfo";
    }


    /**
     * 新生儿登记修改页面
     * @param newbornRec
     * @param model
     * @return String
     * @author  xueliang
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "editFormNewBorn")
    public String editFormNewBorn(NewbornRec newbornRec, Model model) {
        model.addAttribute("newBorn",newbornRec);
        return "modules/nurse/newBornInfo";
    }
	/**
	 * 新生儿住院登记页面
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "inHosReg")
	public String newBorn(Model model,HttpServletRequest request,HttpServletResponse response,NewbornRec newbornRec){
		String wardCode = UserUtils.getUser().getOrgDept().getId();
		String orgId = UserUtils.getOrgId();
		newbornRec.setWardCode(wardCode);
		newbornRec.setOrgId(orgId);
        Page<BaseDto> page=newbornRecService.getPatientList(new Page<BaseDto>(request,response),newbornRec);
		model.addAttribute("page",page);
		model.addAttribute("newbornRec",newbornRec);
		return "modules/nurse/newBornInHos";
	}

	/**
	 * 新生儿信息查询
	 * @param motherId
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "newBonrInfo")
    public String newBonrInfo(NewbornRec newbornRec , String motherId,Model model){
        newbornRec.setVistIdOfMother(motherId);
        newbornRec.setOrgId(UserUtils.getOrgId());
        NewbornRec newBorn= newbornRecService.get(newbornRec);
        model.addAttribute("newBorn",newBorn);
        return "modules/nurse/newBornInfo";
    }

    /**
     * 新生儿住院信息修改页面
     * @return
     * @author xueliang
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "editFormNewBornHos")
    public String editFormNewBornHos(NewbornRec newbornRec,Model model){
        newbornRec.setWardCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
        newbornRec.updateOrgId();
        BaseDto baseDto=  newbornRecService.findNewBornInHos(newbornRec);
        model.addAttribute("newBorn",baseDto);
        return "modules/nurse/newBornInHosInfo";
    }
    /**
     * 新生儿登记保存
     * @param newbornRec
     * @return
     * @author 赵宁
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(NewbornRec newbornRec) {
        StringData data=new StringData();
        newbornRec.updateOrgId();
        newbornRec.setWardCode(UserUtils.getUser().getOrgDept().getId());
        int i=newbornRecService.save(newbornRec);
        if(i>0){
            data.setCode("success");
            data.setData("保存新生儿记录成功");
        }else{
            data.setCode("error");
            data.setData("保存新生儿记录失败");
        }
        return data;
	}

    /**
     * 查询 新生儿登记信息
     * @param model
     * @return
     * @author xueliang
     * @update by CTQ at 2017-07-22 10:54:29
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "getInHosInfo")
    public String getInHosInfo(NewbornRec newbornRec ,Model model,HttpServletRequest request, HttpServletResponse response){
        if(StringUtils.isNotBlank(newbornRec.getId())){
            model.addAttribute("newBorn",newbornRec);
        }else{
            String wardCode = UserUtils.getUser().getOrgDept().getId();
            String orgId = UserUtils.getOrgId();
            newbornRec.setWardCode(wardCode);
            newbornRec.setOrgId(orgId);
            BedRec param = new BedRec();
            param.setPatientId(newbornRec.getPatientIdOfMother());
            param.setVisitId(newbornRec.getVistIdOfMother());
            param.updateOrgId();
            param.setWordCode(wardCode);
            BedRec bedRec = bedRecService.findBedRecByParams(param);
            Page<NewbornRec> page=newbornRecService.findNewBornInHosList(new Page<NewbornRec>(request,response),newbornRec);
            //新生儿住院床号是母亲的床号_子女
            if(page.getList().size() != 0){
                int bedno= page.getList().size()+1;
                newbornRec.setBedNo(bedRec.getBedLabel()+"_"+bedno);
                newbornRec.setBabyInpNo(newbornRec.getMotherInpNo()+"_"+bedno);
            }else{
                newbornRec.setBedNo(bedRec.getBedLabel()+"_1" );
                newbornRec.setBabyInpNo(newbornRec.getMotherInpNo()+"_1" );
            }
            model.addAttribute("newBorn",newbornRec);
        }

        return "modules/nurse/newBornInHosInfo";
    }

	/**
	 * 新生儿登记保存
	 * @param newbornRec
	 * @return
	 * @author 赵宁
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "saveInHosInfo")
	@ResponseBody
	public StringData saveInHosInfo(NewbornRec newbornRec) {
	    StringData data=new StringData();
		int i=newbornRecService.saveInHosInfo(newbornRec);
        if(i>0){
            data.setCode("success");
            data.setData("新生儿住院登记成功");
        }else{
            data.setCode("error");
            data.setData("新生儿住院登记失败");
        }
		return data;
	}

    /**
     * 新生儿住院修改保存
     * @param newbornRec
     * @return
     * @author xueliang
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "updateNewBornHos")
    @ResponseBody
    public StringData updateNewBornHos(NewbornRec newbornRec) {
        StringData data=new StringData();
        int i=newbornRecService.updateNewBornHos(newbornRec);
        if(i>0){
            data.setCode("success");
            data.setData("修改记录成功");
        }else{
            data.setCode("error");
            data.setData("修改记录失败");
        }
        return data;
    }

    /**
     * 根据患者id获取新生婴儿列表
     * @param newbornRec
     * @author  xueliang
     * @update by CTQ at 2017-07-21 15:44:47
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"newBornRecList"})
    public String dialog(NewbornRec newbornRec, HttpServletRequest request, HttpServletResponse response, Model model) {
        newbornRec.setWardCode(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getId():"");
        newbornRec.updateOrgId();
        Page<NewbornRec> page=newbornRecService.findListByVistIdOfMother(new Page<NewbornRec>(request,response),newbornRec);
        model.addAttribute("page", page);
        model.addAttribute("newbornRec", newbornRec);
        return  "modules/nurse/newBornList";
    }


    /**
     * 根据患者id获取新生婴儿住院列表
     * @param newbornRec
     * @author  xueliang
     * @update by CTQ at 2017-07-21 20:42:01
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"newBornHosList"})
    public String newBornHosList(NewbornRec newbornRec,HttpServletRequest request, HttpServletResponse response, Model model) {
        newbornRec.updateOrgId();
        Page<NewbornRec> page=newbornRecService.findNewBornInHosList(new Page<NewbornRec>(request,response),newbornRec);
        model.addAttribute("page", page);
        model.addAttribute("newbornRec", newbornRec);
        return  "modules/nurse/newBornHosList";
    }

    /**
     * 新生儿住院登记-自动补全功能
     * @param q
     * @param newbornRec
     * @param limit
     * @return
     * @author CTQ
     * @date 2017-7-20 20:04:002
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoComplete", ""})
    @ResponseBody
    public List<NewbornRec> autoComplete(String q,NewbornRec newbornRec,String limit) {
        if(q!=null&&!q.equals("")){
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return newbornRecService.findListByParam(q,newbornRec,limit);
    }

	/**
	 * 新生儿入院患者登记页面
	 * @return
	 * @author lizhongyu
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = "bornrecInfo")
	public String bornrecInfo(NewbornRec newbornRec,  Model model,HttpServletRequest request,HttpServletResponse response) {
		String wardCode= UserUtils.getUser().getOrgDept().getId();
		newbornRec.setWardCode(wardCode);
		Page<BaseDto> page=newbornRecService.findornRecList(new Page<BaseDto>(request,response),newbornRec);
		model.addAttribute("newbornRec", newbornRec);
		model.addAttribute("page", page);
		return "modules/doctor/bornrecInfo";
	}

}